package com.jumper.study.common.api;

public class CommonResult<T> {
    private long code;

    private String message;

    private T data;

    public CommonResult() {

    }

    public CommonResult(long code) {
        this.code = code;
        message = "";
        data = null;
    }

    public CommonResult(long code, String message) {
        this.code = code;
        this.message = message;
        data = null;
    }

    public CommonResult(long code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public static <T> CommonResult<T> success() {
        return new CommonResult<T>(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage());
    }

    public static <T> CommonResult<T> success(T data) {
        return new CommonResult<T>(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage(), data);
    }

    public static <T> CommonResult<T> failed(long code, String message) {
        return new CommonResult<T>(code, message);
    }

    public static <T> CommonResult<T> failed(IErrorCode code) {
        return new CommonResult<>(code.getCode(), code.getMessage());
    }

    public static <T> CommonResult<T> failed(String message) {
        return new CommonResult<T>(ResultCode.FAILED.getCode(), message);
    }

    public static <T> CommonResult<T> failed() {
        return new CommonResult<T>(ResultCode.FAILED.getCode(), ResultCode.FAILED.getMessage());
    }

    public static <T> CommonResult<T> validateFailed(String message) {
        return new CommonResult<T>(ResultCode.VALIDATE_FAILED.getCode(), message);
    }

    public static <T> CommonResult<T> unauthorized(T data) {
        return new CommonResult<T>(ResultCode.UNAUTHORIZED.getCode(), ResultCode.UNAUTHORIZED.getMessage(), data);
    }

    public static <T> CommonResult<T> forbidden(T data) {
        return new CommonResult<T>(ResultCode.FORBIDDEN.getCode(), ResultCode.FORBIDDEN.getMessage(), data);
    }

    public static <T> CommonResult<T> notFound() {
        return CommonResult.notFound(ResultCode.NOT_FOUND.getMessage());
    }

    public static <T> CommonResult<T> notFound(String message) {
        return new CommonResult<>(ResultCode.NOT_FOUND.getCode(), message);
    }

    public long getCode() {
        return code;
    }

    public String getMessage() {
        return message;
    }

    public T getData() {
        return data;
    }
}
